<?php
/**
 * Created by PhpStorm.
 * User: 冰封~零度
 * Date: 2018/10/11
 * Time: 10:26
 */

namespace app\mobile\controller;

use think\Controller;
use think\Db;
use think\Session;

class Center extends Controller
{
    // 个人中心
    public function index()
    {
        // 获取用户个人信息
        $userInf = Session::get('puser');
        // 查询用户收藏
        $collectNum = count(Db::table('zz_collect')->where('zz_user_id',$userInf['zz_id'])->select());
        // 查询用户关注
        $followNum = count(Db::table('zz_follow')->where('zz_user_id',$userInf['zz_id'])->select());
        // 查询用户问题
        $problem = Db::table('zz_problem')->where('zz_uid',$userInf['zz_id'])->select();
        $problemNum = count($problem);

        return view('center/center',[
            'userInf'       => $userInf,
            'collectNum'    => $collectNum,
            'followNum'     => $followNum,
            'problemNum'    => $problemNum,
            'problem'       => $problem
        ]);
    }

    // 个人中心信息修改
    public function docenter($id)
    {
        // 查询当前id 下用户的信息
        $user = Db::table('zz_user')->where('zz_id',$id)->find();
        return view('center/tocenter',[
            'user' => $user
        ]);
    }

    // 处理个人中心信息修改
    public function docenterif()
    {
        // 接收数据
        $tel = input('tel');
        $name = input('name');
        $email = input('email');
        $address = input('address');
        $sex = input('sex');
        // 确认数据是否修改
        $data = Db::table('zz_user')->where('zz_tel',$tel)->find();
        if ($data['zz_name'] == $name && $data['zz_sex'] == $sex && $data['zz_email'] == $email && $data['zz_address'] == $address){
            $res['status'] = 2;
            return json($res);
        } else {
            $data = [];
            $data['zz_name'] = $name;
            $data['zz_sex'] = $sex;
            $data['zz_email'] = $email;
            $data['zz_address'] = $address;
            Db::table('zz_user')->where('zz_tel',$tel)->update($data);
            $res['status'] = 1;
            return json($res);
        }

        $res['status'] = 1;
        return json($res);
    }

    // 修改密码页面
    public function dopass($id)
    {
        return view('center/addpass',[
            'id' => $id
        ]);
    }

    // 修改密码处理
    public function addpass()
    {
        // 接收数据
        $userid = input('userid');
        $oldpass = input('oldpass');
        $newpass = input('newpass');
        $newpassagain = input('newpassagain');
        // 查询密码是否正确
        $password = Db::table('zz_user')->field('zz_pass')->where('zz_id',$userid)->find();
        if ($oldpass != $password['zz_pass']){
            // 原密码输入错误
            $res['status'] = 1;
            return json($res);
        } else if ($newpass != $newpassagain){
            // 两次密码输入不一致
            $res['status'] = 2;
            return json($res);
        } else if ($oldpass == $newpass){
            // 原密码和新密码一样
            $res['status'] = 3;
            return json($res);
        } else {
            // 所有的都正确
            $data['zz_pass'] = $newpass;
            Db::table('zz_user')->where('zz_id',$userid)->update($data);
            // 删除Session
            Session::delete('puser');
            $res['status'] = 4;
            return json($res);
        }






    }
}